package com.example.test_8_video;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class FileShowActivity extends AppCompatActivity {
    TextView textView;
    Button display;
    ListView listview;
    AlertDialog.Builder builder;
    AlertDialog ad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_show);
        textView = (TextView) findViewById(R.id.tv_1);
        display = (Button)findViewById(R.id.display);
        listview = (ListView)findViewById(R.id.listview);




        Intent intent = getIntent();
        final String path = intent.getStringExtra("path");
        textView.setText("输入的文件绝对路径为："+path);
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET}, 0x123);
        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent1 = new Intent(FileShowActivity.this,VideoViewActivity.class);
                    intent1.putExtra("path",path);
                    startActivity(intent1);
            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode==0x123&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
        {
            listview = new ListView(this);    //搜索手机中的视频文件
            ContentResolver contentResolver = this.getContentResolver();
            Cursor c = contentResolver
                    .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                            new String[]{MediaStore.Video.Media.TITLE,
                                    MediaStore.Video.Media.DURATION,
                                    MediaStore.Video.Media._ID,
                                    MediaStore.Video.Media.DISPLAY_NAME,
                                    MediaStore.Video.Media.DATA},
                            null, null, null);
            System.out.println("---------"+c.getCount()+"------------");
            if (c == null || c.getCount() == 0)     //如果没有搜索到视频，显示存储列表为空...
            {
                builder = new AlertDialog.Builder(this);
                builder.setMessage("存储列表为空...").setPositiveButton("确定", null).setNegativeButton("取消",null);
                ad = builder.create();
                ad.show();
            }
        }
    }
}
